/*
 * Library.java
 *
 * Created on August 31, 2006, 5:04 PM
 *
 * (C) 2006 R. Alexander Milowski
 */

package com.smallx.xproc;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.infoset.xml.Name;

/**
 *
 * @author alex
 */
public class Library extends ModelItem {
   
   Map<Name,ComponentType> types;
   
   /** Creates a new instance of Library */
   public Library(Name name) {
      super(name);
      this.types = new HashMap<Name,ComponentType>();
   }
   
   public void addType(ComponentType type) 
      throws ModelErrorException
   {
      if (types.get(type.getName())!=null) {
         throw new ModelErrorException("Component type "+type.getName()+" already defined.");
      }
      types.put(type.getName(),type);
   }
   
   public Set<Name> getTypeNames() {
      return types.keySet();
   }
   
   public ComponentType getComponentDefinition(Name name) {
      return types.get(name);
   }
   
   public void importLibrary(Library library)
      throws ModelErrorException
   {
      boolean failed = false;
      ModelErrorException error = new ModelErrorException("Failed to import library due to conflicting names.",library);
      for (Name typeName : library.getTypeNames()) {
         try {
            addType(library.getComponentDefinition(typeName));
         } catch (ModelErrorException ex) {
            failed = true;
            error.addError(ex.getMessage());
         }
      }
      if (failed) {
         throw error;
      }
   }      
   
   
}
